<?php


namespace app\service;


use app\entity\SysUserEntity;
use app\entity\SysUserRoleEntity;
use app\models\response\RespRoleDetail;

class SysUserRoleService
{

    public function queryRoleIdList($userId) {
        $sql = " select a.role_id , b.role_name from sys_user_role a, sys_role b where a.user_id=:id AND a.role_id = b.role_id";
        $res = SysUserEntity::getDb()->createCommand($sql,[":id"=>$userId])->queryAll();


        if (!$res) return null;
        $arr = [];
        foreach ($res as $detail) {
            $d = new RespRoleDetail();
            $d->setRoleId($detail["role_id"]);
            $d->setRoleName($detail["role_name"]);
            $arr[] = $d;
        }
        return $arr;
    }

    /**
     * @param $userId
     * @param $roleIdList
     */
    public function saveOrUpdate($userId, $roleIdList) {
        SysUserRoleEntity::deleteAll(["user_id"=>$userId]);
        foreach ($roleIdList as $id) {
            $entity = new SysUserRoleEntity();
            $entity->setUserId($userId);
            $entity->setRoleId($id);
            $entity->save($entity);
        }
    }





}